package com.wenzhiguo.myheadline.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wenzhiguo.myheadline.Bean.NewsSummary;
import com.wenzhiguo.myheadline.MyBaseAdapter;
import com.wenzhiguo.myheadline.R;
import com.wenzhiguo.myheadline.WebView_Activity;
import com.wenzhiguo.myheadline.view.XListView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


public class NewFragment extends Fragment implements XListView.IXListViewListener {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    private View view;
    private XListView xlv;
    private ArrayList<NewsSummary> al;
    int page = 0;

    public NewFragment() {
    }

    public static NewFragment newInstance(String param1, String param2) {
        NewFragment fragment = new NewFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_new, container, false);
        //控件
        initView();
        //请求数据
        Request(mParam2, 0);

        return view;
    }

    private void initView() {
        xlv = (XListView) view.findViewById(R.id.xlv);
        al = new ArrayList<>();
        xlv.setPullRefreshEnable(true);
        xlv.setPullLoadEnable(true);
        xlv.setXListViewListener(this);

        xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), WebView_Activity.class);
                intent.putExtra("url", al.get(position - 1).url_3w);
                startActivity(intent);
            }
        });
    }

    private void Request(final String a, int b) {
        String url = RequestUrl(a, b);
        //请求
        x.http().get(new RequestParams(url), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                //解析
                Map<String, List<NewsSummary>> map = new Gson().fromJson(result, new TypeToken<Map<String, List<NewsSummary>>>() {
                }.getType());

                List<NewsSummary> list = map.get(a);
                //添加到list
                al.addAll(list);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                MyBaseAdapter adapter = new MyBaseAdapter(getActivity(), al);
                //适配
                if (page == 0) {
                    xlv.setAdapter(adapter);

                } else {
                    adapter.notifyDataSetChanged();
                }

            }
        });
    }

    private String RequestUrl(String c, int d) {
        String url = String.format("http://c.m.163.com/nc/article/headline/%1$s/%2$s-20.html", c, d);
        return url;
    }

    @Override
    public void onRefresh() {
        page = 0;
        al.clear();
        Request(mParam2, page);
        //停止刷新和加载
        xlv.stopRefresh();
        xlv.stopLoadMore();
        //时间
        getDate();
    }

    @Override
    public void onLoadMore() {
        page += 20;
        Request(mParam2, page);
        //停止刷新和加载
        xlv.stopRefresh();
        xlv.stopLoadMore();
        getDate();
    }

    private void getDate() {
        //时间
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd  hh-mm-ss");
        String str = format.format(date);
        xlv.setRefreshTime(str);
    }
}
